﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using DSkin.Controls;
using System.Xml;
using System.Collections;

namespace DS.Controls
{
    public partial class DSTreeView : DSkinUserControl
    {
        public DSTreeView()
        {
            InitializeComponent();
            
        }

        private void DSTreeView_Load(object sender, EventArgs e)
        {
            SetScrollBarStyle();
        }

        private void SetScrollBarStyle()
        {
            treeviewfolder.InnerScrollBar.ShowArrow = true;
            treeviewfolder.InnerScrollBar.BackColor = Color.Transparent;
            treeviewfolder.InnerScrollBar.Fillet = true;
            treeviewfolder.InnerScrollBar.Width = 10;
        }

        delegate void RunTaskDelegate();
        //delegate void SetProgressbarCallback(object sender, AioApp.Upload.ZipEventArgs e);



        private Common.ServerProfile serverProfile;

        //private void Uploader_Load(object sender, EventArgs e)
        //{
        //    serverProfile = new SC.ServerProfile();
        //}

        /// <summary>
        /// 加载Uploader
        /// </summary>
        public void LoadDir()
        {
            //GetDirs();
            if (serverProfile == null)
            {
                serverProfile = new Common.ServerProfile(true);
            }

            LoadDirs(serverProfile.PathList);
            
            this.Visible = true;
        }

        private void LoadDirs(List<string> dirs)
        {
            treeviewfolder.Nodes.Clear();
            TreeNode rootNode = new TreeNode("我的电脑",
                IconIndexes.MyComputer, IconIndexes.MyComputer);  //载入显示 选择显示

            //循环遍历计算机所有逻辑驱动器名称(盘符)
            foreach (string driver in Environment.GetLogicalDrives())
            //foreach (DriveInfo drive in drivers)
            {
                //实例化DriveInfo对象 命名空间System.IO
                var dir = new DriveInfo(driver);
                DSkinTreeViewNode tNode = new DSkinTreeViewNode();
                tNode.Text = dir.Name.Split(':')[0];
                tNode.Name = dir.Name;
                tNode.Tag = tNode.Name;
                //tNode.ImageIndex = IconIndexes.FixedDrive;         //获取结点显示图片
                //tNode.SelectedImageIndex = IconIndexes.FixedDrive; //选择显示图片
                treeviewfolder.Nodes.Add(tNode);                    //加载驱动节点
                tNode.Nodes.Add(new DSkinTreeViewNode());
            }
            
            //遍历配置文件指定网盘
            foreach (string folder in serverProfile.PathList)
            {
                DSkinTreeViewNode tNode = new DSkinTreeViewNode();
                tNode.Text = folder;
                tNode.Name = folder;
                tNode.Tag = tNode.Name;
                //tNode.ImageIndex = IconIndexes.FixedDrive;         //获取结点显示图片
                //tNode.SelectedImageIndex = IconIndexes.FixedDrive; //选择显示图片
                treeviewfolder.Nodes.Add(tNode);                    //加载驱动节点
                

                tNode.Nodes.Add(new DSkinTreeViewNode());           //加叶子节点为了能展开
            }
            rootNode.Expand();
        }

        private void dSkinTreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            if (treeviewfolder.SelectedNode.CheckState == CheckState.Unchecked && treeviewfolder.SelectedNode.Nodes.Count == 1)//前面添加了一个默认节点 点击的时候才加载后面的层
            {
                TreeViewItems.AddNode(treeviewfolder.SelectedNode);
            }
            
        }

        /// <summary>
        /// 自定义类TreeViewItems 调用其Add(TreeNode e)方法加载子目录
        /// </summary>
        /// <summary>
        /// 自定义类TreeViewItems 调用其Add(TreeNode e)方法加载子目录
        /// </summary>
        public static class TreeViewItems
        {
            public static void AddNode(DSkinTreeViewNode e)
            {
                //try..catch异常处理
                try
                {
                    //判断"我的电脑"Tag 上面加载的该结点没指定其路径
                    if (e.Text != "我的电脑")
                    {
                        e.Nodes.Clear();                               //清除空节点再加载子节点
                        DSkinTreeViewNode tNode = e;                            //获取选中\展开\折叠结点
                        string path = tNode.Name;                      //路径  

                        //获取"我的文档"路径
                        if (e.Text == "我的文档")
                        {
                            path = Environment.GetFolderPath           //获取计算机我的文档文件夹
                                (Environment.SpecialFolder.MyDocuments);
                        }

                        //获取指定目录中的子目录名称并加载结点
                        string[] dics = Directory.GetDirectories(path);
                        //Array.Sort(dics, new FilesNameComparerClass());

                        foreach (string dic in dics)
                        {
                            DSkinTreeViewNode subNode = new DSkinTreeViewNode(); //实例化
                            subNode.Text = new DirectoryInfo(dic).Name;
                            subNode.Name = new DirectoryInfo(dic).FullName;               //完整目录
                            subNode.Tag = subNode.Name;

                            if (subNode.Text.Equals("images") || subNode.Text.Equals("Rescource"))
                            {
                                //排除treeview显示 资源里的images 和 Resource 文件夹
                                if (Directory.GetDirectories(subNode.Name).Length == 0)
                                return;
                            }

                            tNode.Nodes.Add(subNode);
                            subNode.Nodes.Add(new DSkinTreeViewNode());//叶子节点
                        }
                    }
                }
                catch (Exception msg)
                {
                    MessageBox.Show(msg.Message);                   //异常处理
                    Common.Log.Error(msg.Message);
                }
            }
        }

        /// <summary>
        /// IconIndexs类 对应ImageList中5张图片的序列
        /// </summary>
        public class IconIndexes
        {
            public const int MyComputer = 0;      //我的电脑
            public const int ClosedFolder = 1;    //文件夹关闭
            public const int OpenFolder = 2;      //文件夹打开
            public const int FixedDrive = 3;      //磁盘盘符
            public const int MyDocuments = 4;     //我的文档
        }




        private void dSkinTreeView1_DoubleClick(object sender, EventArgs e)
        {
            //treeviewfolder.SelectedNode.NodesOpened = !treeviewfolder.SelectedNode.NodesOpened;
        }




        /// <summary>
        /// 查询TreeView下节点被checked的数目
        /// </summary>
        /// <param name="treev"></param>
        /// <returns></returns>
        private List<DSkinTreeViewNode> GetTreeViewNodeChecked(DSkinTreeView treev)
        {
            
            foreach (DSkinTreeViewNode item in treev.Nodes)
            {
                GetNodeChecked(item);
            }
            return nodeCheckedList;
        }

        private List<DSkinTreeViewNode> nodeCheckedList = new List<DSkinTreeViewNode>();


        /// <summary>
        /// 查询treeNode节点下有多少节点被选中（递归实现，不受级数限制）
        /// </summary>
        /// <param name="dSkinTreeViewNode"></param>
        /// <returns></returns>
        public void GetNodeChecked(DSkinTreeViewNode dSkinTreeViewNode)
        {
            if (dSkinTreeViewNode.CheckState == CheckState.Checked)
            {
                if (!string.IsNullOrEmpty(dSkinTreeViewNode.Name))
                    nodeCheckedList.Add(dSkinTreeViewNode);
            }
            foreach (DSkinTreeViewNode item in dSkinTreeViewNode.Nodes)
            {
                GetNodeChecked(item);
            }
        }

        //private void dSkinButton1_Click(object sender, EventArgs e)
        //{
        //    List<DSkinTreeViewNode> dSkinTreeViewNodes = GetTreeViewNodeChecked(this.treeviewfolder);
        //    foreach(DSkinTreeViewNode node in dSkinTreeViewNodes)
        //    {
        //        Console.WriteLine(node.Name);
        //    }
        //}

        public List<DSkinTreeViewNode> GetSelectedItems()
        {
            return GetTreeViewNodeChecked(this.treeviewfolder);
        }


    }
}
